#include "./glApp.h"
#include "../../inc/skyApplication.h"
#ifdef _GLES_

//#pragma comment(lib,".\\code\\xlib\\adapter\\thirdParty\\win32\\libraries\\libgles_cm.lib")
namespace GL
{
	GLContext context;

	Sky::Bool CreateGLContext(Sky::Setting* setting)
	{
		EGLint		aEGLAttributes[] = {
			EGL_RED_SIZE, 8,
			EGL_GREEN_SIZE, 8,
			EGL_BLUE_SIZE, 8,
			EGL_ALPHA_SIZE,8,
			EGL_DEPTH_SIZE, 16,
			EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,

			EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
			EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE,

			EGL_NONE
		};

		EGLint		aEGLContextAttributes[] = {
			EGL_CONTEXT_CLIENT_VERSION, 1,
			EGL_NONE
		};

		HDC hdc = GetDC(setting->getWindow());
		context.m_hwnd = setting->getWindow();

		EGLint	   Width;
		EGLint	   Height;

		EGLint nConfigs;
		context.m_eglDisplay = eglGetDisplay(hdc);
		do 
		{
			E_Break_If(!eglInitialize(context.m_eglDisplay, NULL, NULL));

			E_Break_If(!eglChooseConfig(context.m_eglDisplay, aEGLAttributes, context.m_eglConfig, 1, &nConfigs));

			E_Break_If((context.m_eglContext = eglCreateContext(context.m_eglDisplay, context.m_eglConfig[0], EGL_NO_CONTEXT, aEGLContextAttributes))
				== EGL_NO_CONTEXT);

			E_Break_If((context.m_eglSurface = eglCreateWindowSurface(context.m_eglDisplay, context.m_eglConfig[0], (NativeWindowType)context.m_hwnd, 0))
				== EGL_NO_SURFACE);

			eglQuerySurface(context.m_eglDisplay, context.m_eglSurface,EGL_WIDTH,  (EGLint*)&Width);

			eglQuerySurface(context.m_eglDisplay, context.m_eglSurface,EGL_HEIGHT, (EGLint*)&Height);

			setting->setWidth(Width);
			setting->setHeight(Height);

			E_Break_If(!eglMakeCurrent(context.m_eglDisplay, context.m_eglSurface, context.m_eglSurface, context.m_eglContext));

			xAssert(GL_NO_ERROR == glGetError());

			

			int N_PLANE_W = Width;
			int N_PLANE_H = Height;
			glMatrixMode(GL_PROJECTION);
			glOrthox(-(N_PLANE_W/2)<<16, (N_PLANE_W/2)<<16, -(N_PLANE_H/2)<<16, (N_PLANE_H/2)<<16, -128<<16, 128<<16);
			//glViewport(-(N_PLANE_W/2),  -(N_PLANE_H/2), (GLsizei)Width/2, (GLsizei)Height/2);

			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
			
			return Sky::vTrue;
		} while (0);
		xAssert(vNull);
		return Sky::vFalse;
	};

	Sky::Bool DeleteGLContext()
	{
		// Exit.
		do 
		{
			eglMakeCurrent(context.m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
			eglDestroyContext(context.m_eglDisplay, context.m_eglContext);
			//eglDestroySurface(context.m_eglDisplay, context.m_eglSurface);
			eglTerminate(context.m_eglDisplay);
			return Sky::vTrue;
		} while (0);
		xAssert(vNull);
		return Sky::vFalse;;
	};
	GLContext* getContext()
	{
		return &context;
	}
	EGLSurface CreateRenderSurface(GLint w,GLint h,GLuint Format_)
	{
		EGLint list[]=
		{
			// Specify the size of the surface.
			EGL_WIDTH, w,
			EGL_HEIGHT, h,
			// Target for the texture to store in the pbuffer.
			EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
			// The format of the texture that will be created when the pBuffer is bound to a texture.
			EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,

			// Signal the end.
			EGL_NONE
		};
		
		EGLSurface surf;
		do 
		{
			E_Break_If((surf = eglCreatePbufferSurface(context.m_eglDisplay, context.m_eglConfig[0], list)) == EGL_NO_SURFACE);
			return surf;

		} while (0);
		
		xAssert(vNull);

		return 0;
	}

	Sky::Void DestroyRenderSurface(EGLSurface surf)
	{
		eglDestroySurface(context.m_eglDisplay, surf);
	}

	Sky::Bool BindTexImage( EGLSurface surf )
	{
		do 
		{
			E_Break_If(!eglBindTexImage(context.m_eglDisplay, surf, EGL_BACK_BUFFER));
			return Sky::vTrue;
		} while (0);
		xAssert(vNull);
		return Sky::vFalse;;
	}

	Sky::Bool ReleaseTexImage( EGLSurface surf )
	{
		eglReleaseTexImage(context.m_eglDisplay, surf,EGL_BACK_BUFFER);
		return Sky::vTrue;
	}

}

namespace Sky
{
	
	using namespace GL;

	Bool Application::Initialise( Setting* setting )
	{
		return CreateGLContext(setting);
	}

	Bool Application::Finalise( Setting* setting )
	{
		return DeleteGLContext();
	}
}

#endif
